const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const User = sequelize.define('User', {
  user_id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    field: 'user_id'
  },
  username: { 
    type: DataTypes.STRING, 
    allowNull: false 
  },
  email: { 
    type: DataTypes.STRING, 
    allowNull: false, 
    unique: true 
  },
  password: { 
    type: DataTypes.STRING, 
    allowNull: false 
  },
  avatar_url: {
    type: DataTypes.STRING,
    allowNull: true, // 头像 URL 可以为空
    field: 'avatar_url'
  }
}, {
  tableName: 'Users',
  timestamps: true
});

User.associate = (models) => {
  User.belongsToMany(models.City, {
    through: models.UserCity,
    foreignKey: 'user_id',
    otherKey: 'city_id'
  });
};

module.exports = User;